using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

namespace LS
{
    /// <summary>
    /// 组件用来在Camera上绘制矩形选框，设置Component.Enable来控制其运行状态
    /// </summary>
    [RequireComponent(typeof(Camera))]
    public class CameraSelectionPainter : MonoBehaviour
    {
        private Vector2 m_StartPoint = Vector2.zero;
        private Vector2 m_EndPoint = Vector2.zero;

        /// <summary>
        /// 视窗坐标起点
        /// </summary>
        public Vector2 StartViewPoint
        {
            get
            {
                return m_StartPoint;
            }
            set
            {
                m_StartPoint = value;
            }
        }

        /// <summary>
        /// 视窗坐标终点
        /// </summary>
        public Vector2 EndViewPoint
        {
            get
            {
                return m_EndPoint;
            }
            set
            {
                m_EndPoint = value;
            }
        }

        /// <summary>
        /// 屏幕坐标起点
        /// </summary>
        public Vector2 StartScreenPoint
        {
            get
            {
                return Camera.ViewportToScreenPoint(m_StartPoint);
            }
            set
            {
                m_StartPoint = Camera.ScreenToViewportPoint(value);
            }
        }

        /// <summary>
        /// 屏幕坐标终点
        /// </summary>
        public Vector2 EndScreenPoint
        {
            get
            {
                return Camera.ViewportToScreenPoint(m_EndPoint);
            }
            set
            {
                m_EndPoint = Camera.ScreenToViewportPoint(value);
            }
        }

        [Tooltip("选框的边框色")]
        [SerializeField] private Color m_SelectionFrame = new Color(0, 1, 0, 1);
        [Tooltip("选框的中间色")]
        [SerializeField] private Color m_SelectionInside = new Color(0, 1, 0, 0.1f);
        [Tooltip("选框使用的渲染材质")]
        [SerializeField] private Material mat;

        private Camera m_Camera = null;

        /// <summary>
        /// 绘制选框的相机组件
        /// </summary>
        public Camera Camera
        { 
            get
            {
                if (m_Camera == null)
                {
                    m_Camera = GetComponent<Camera>();
                }
                return m_Camera;
            } 
        }

        private void OnEnable()
        {
            UnityEngine.Rendering.RenderPipelineManager.endCameraRendering -= GLPainter;
            UnityEngine.Rendering.RenderPipelineManager.endCameraRendering += GLPainter;
        }

        private void OnDisable()
        {
            UnityEngine.Rendering.RenderPipelineManager.endCameraRendering -= GLPainter;
        }

        private void GLPainter(ScriptableRenderContext context, Camera cameras)
        {
            if (cameras != this.m_Camera)
            {
                return;
            }
            Vector2 start = StartViewPoint;
            Vector2 end = EndViewPoint;
            GL.PushMatrix();
            mat.SetPass(0);
            GL.LoadOrtho();
            GL.Begin(GL.QUADS);
            GL.Color(m_SelectionInside);
        
            GL.Vertex3(start.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);
            GL.End();

            GL.Begin(GL.LINES);
            GL.Color(m_SelectionFrame);
            GL.Vertex3(start.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);
            GL.Vertex3(start.x, start.y, 0);
            GL.End();
            GL.PopMatrix();
        }
    }
}
